package controllers

import (
	"gin-worked/app/system/service"
	"gin-worked/app/system/validate"
	"gin-worked/work"
	"github.com/gin-gonic/gin"
)

type DataSheetController struct {
}

// List 获取数据表列表
func (ds DataSheetController) List(c *gin.Context) {
	param := c.MustGet("params").(map[string]string)
	res := service.DataSheetList(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Add 数据表添加
func (ds DataSheetController) Add(c *gin.Context) {

	var add validate.DataSheetAdd
	if err := c.ShouldBind(&add); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.DataSheetAdd(param["Title"], param["Type"], param["Length"], param["Remark"], param["DefaultValue"], param["ParentId"], param["Status"], param["IsValidation"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Info 获取详情
func (ds DataSheetController) Info(c *gin.Context) {
	var info validate.DataSheetInfo
	if err := c.ShouldBind(&info); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.DataSheetInfo(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// Edit 编辑
func (ds DataSheetController) Edit(c *gin.Context) {
	var edit validate.DataSheetEdit
	if err := c.ShouldBind(&edit); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.DataSheetEdit(param["Id"], param["Title"], param["Type"], param["Length"], param["Remark"], param["DefaultValue"], param["ParentId"], param["Status"], param["IsValidation"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
}

// Del 删除
func (ds DataSheetController) Del(c *gin.Context) {
	var info validate.DataSheetInfo
	if err := c.ShouldBind(&info); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}
	param := c.MustGet("params").(map[string]string)
	res := service.DataSheetDel(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// SelectSql 查询数据库添加到数据表
func (ds DataSheetController) SelectSql(c *gin.Context) {
	res := service.SelectSql()
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// CreateSql 创建数据表
func (ds DataSheetController) CreateSql(c *gin.Context) {
	var info validate.DataSheetInfo
	if err := c.ShouldBind(&info); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.CreateSql(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}

// CreateFile 创建对应的controller validate service model
func (ds DataSheetController) CreateFile(c *gin.Context) {
	var info validate.DataSheetInfo
	if err := c.ShouldBind(&info); err != nil {
		work.ReturnResult(c, 0, err.Error(), "")
		return
	}

	param := c.MustGet("params").(map[string]string)
	res := service.CreateFile(param["Id"])
	work.ReturnResult(c, res.Code, res.Msg, res.Data)
	return
}
